Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

335
Visualizações
I get "SyntaxError:Cannot use import statement outside a module", after adding {"type": "module"}

I'm trying to import another .js file in my main server file "App.js", but I keep getting the syntax error:

SyntaxError:Cannot use import statement outside a module

I did include the "type": "module" in my package.json file.

You can see the line where I'm trying to import the file here;

import {Read as readSensor} from './Sensor.js';

The function that I'm trying to import looks like this:

export async function Read() {
    const res = await fetch(SensorUrl);
    if(res.ok){
        return await res.json();
    }
    else{
        throw new Error('Bad Reading');
    }
}

AFAIK, the syntax of my import/exports is correct, so I really cannot figure out what I'm doing wrong.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you are using node js v14 or above, try these:

add "type": "module" in your package.json use --experimental-modules when launching your app, eg: node --experimental-modules index.js

Or you can try this also:

add "type": "module" in your package.json and rename your file with a .mjs extension, the file will look like something Sensor.mjs.

Feel free to add a comment if you face any issue, also pls add your package.json file with node version for better debugging.

Have a look at this link, it might help you.

about 4 years ago · Juan Pablo Isaza Relatório

0

So, I figured it out, and it turns out I'm dumb. I was trying to run this with an outdated version of node.js (12.14.1). It worked as soon as I updated to version 16.14.2. So today I have learned the very valuable lesson of keeping my stuff updated. Thank you for the answers!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda